#ifndef CORE_AABB_H
#define CORE_AABB_H
#include "Types.h"
#include "Ray.h"
namespace RayTracer
{
	class AABB 
	{
	public:
		AABB() {}
		AABB(const vec3& a, const vec3& b) { minimum = a; maximum = b; }

		vec3 Min() const { return minimum; }
		vec3 Max() const { return maximum; }

		bool Hit(const Ray& r, float t_min, float t_max) const 
		{
			for (int a = 0; a < 3; a++) {
				auto invD = 1.0f / r.direction()[a];
				auto t0 = (Min()[a] - r.origin()[a]) * invD;
				auto t1 = (Max()[a] - r.origin()[a]) * invD;
				if (invD < 0.0f)
					std::swap(t0, t1);
				t_min = t0 > t_min ? t0 : t_min;
				t_max = t1 < t_max ? t1 : t_max;
				if (t_max <= t_min)
					return false;
			}
			return true;
		}

		static AABB SurroundingBox(AABB box0, AABB box1)
		{
			vec3 small(fmin(box0.Min().x, box1.Min().x),
				fmin(box0.Min().y, box1.Min().y),
				fmin(box0.Min().z, box1.Min().z));

			vec3 big(fmax(box0.Max().x, box1.Max().x),
				fmax(box0.Max().y, box1.Max().y),
				fmax(box0.Max().z, box1.Max().z));

			return AABB(small, big);
		}

		vec3 minimum;
		vec3 maximum;
	};


}
#endif